class Player
	def play_turn(warrior)
		health = 5
		
		if warrior.health < 5
			simple_rest(warrior)
		elsif simple_rescue?(warrior)
			puts warrior.health
		elsif simple_slugger?(warrior) && simple_tick?(warrior)
			if warrior.health < 5
				warrior.rest!
			else
				puts "detonate 1"
				warrior.detonate!
			end
		else
			simple_attack(warrior)
		end
	end
	
	def simple_attack(warrior)
		directions = [:left,:right,:forward,:backward]
		stair = warrior.direction_of_stairs
		if warrior.feel(stair).enemy?
			#更多
			more = false
			directions.each{|item| 
			if item != stair && warrior.feel(item).enemy?
				puts "bind enemy"
				warrior.bind!(item)
				more = true
				break
			end
			}
			if !more
				puts "attack 1"
				#if warrior.feel.empty?
					#warrior.walk!
				if warrior.feel.enemy?
					warrior.attack!()
				elsif warrior.feel(:right).enemy?
					warrior.attack!(:right)
				elsif warrior.feel(:left).enemy?
					warrior.attack!(:left)
				elsif warrior.feel(:backward).enemy?
					warrior.attack!(:backward)
				end
			end
		elsif warrior.look(stair)[0].to_s == "nothing"
			if warrior.feel(:left).enemy?
				warrior.attack!(:left)
			elsif warrior.feel(:right).enemy?
				warrior.attack!(:right)
			elsif warrior.feel.enemy?
				warrior.attack!
			else
				wListen = warrior.listen
				if warrior.feel(:left).to_s == "Sludge"
					warrior.attack!(:left)
				elsif warrior.feel(:right).to_s == "Sludge"
					warrior.attack!(:right)
				elsif warrior.feel(:forward).to_s == "Sludge"
					warrior.attack!
				elsif warrior.feel(:backward).to_s == "Sludge"
					warrior.attack!(:backward)
				elsif wListen.size > 0
					sludge = wListen[0]
					dir = warrior.direction_of(sludge)
					sludgeCount = 0
					captiveCount = 0
					thickCount = 0
					wListen.each{|item| 
						if item.to_s == "Captive"
							captiveCount = captiveCount + 1
						end
						if item.to_s == "Sludge" 
							sludgeCount = sludgeCount + 1
						end
						if item.to_s ==  "Thick Sludge"
							thickCount = thickCount + 1
						end
					}
					if warrior.distance_of(sludge) == 1
						puts "attack sludge"
						warrior.attack!(dir)
					elsif  sludgeCount + thickCount > 0 && captiveCount == 0 && warrior.health < 10 * sludgeCount + 15 * thickCount  && warrior.health < 20
						puts "rest again"
						warrior.rest!
					else
						puts "walk to sludge"
						warrior.walk!(dir)
					end
				else
					puts "walk_1"
					warrior.walk!(stair)
				end
			end
		else
			puts "attack 2"
			warrior.attack!(stair)
		end
	end
	
	def simple_rest(warrior)
		if !warrior.feel(:left).enemy? && !warrior.feel(:right).enemy? && !warrior.feel(:forward).enemy? && !warrior.feel(:backward).enemy?
			wListen = warrior.listen
			ticking = false
			enemy = false
			direction = :forward 
			wListen.each{|item|
				if item.ticking?
					ticking = true
					direction = warrior.direction_of(item)
				elsif item.enemy?
					enemy = true
				end
			}
			#没有敌人有炸弹不休息
			if ticking && !enemy
				puts "no enemy"
				if warrior.feel(direction).captive?
					warrior.rescue!(direction)
				else
					warrior.walk!(direction)
				end
			else
				puts "simple rest"
				warrior.rest!
			end
		elsif warrior.feel(:backward).empty?
			warrior.walk!(:backward)
		elsif warrior.feel(:forward).empty?
			warrior.walk!
		elsif warrior.feel(:right).empty?
			warrior.walk!(:right)
		elsif warrior.feel(:left).empty?
			warrior.walk!(:left)
		end
	end
	
	def simple_slugger?(warrior)
		warrior.listen.each{|item|
				if item.enemy?
					return true
				end
			}
		return false
	end
	
	def simple_tick?(warrior)
		warrior.listen.each{|item|
			if item.ticking?
				return true
			end
		}
		return false
	end
	
	def simple_captive?(warrior)
		warrior.listen.each{|item|
			if item.captive?
				return true
			end
		}
		return false
	end
	
	def simple_rescue?(warrior)
		if warrior.feel(:left).captive? && warrior.look(:left)[0].to_s == "Captive"
			puts "rescue left"
			warrior.rescue!(:left)
			return true
		elsif warrior.feel(:right).captive?  && warrior.look(:right)[0].to_s == "Captive"
			puts "rescue right"
			warrior.rescue!(:right)
			return true
		elsif warrior.feel(:forward).captive?  && warrior.look(:forward)[0].to_s == "Captive"
			puts warrior.listen
			puts "rescue forward"
			warrior.rescue!(:forward)
			return true
		elsif warrior.feel(:backward).captive? && warrior.look(:backward)[0].to_s == "Captive"
			puts "rescue backward"
			warrior.rescue!(:backward)
			return true
		else
			warrior.listen.each{|item|
				if item.to_s == "Captive"
					dir = warrior.direction_of(item)
					if warrior.feel(dir).enemy?
						if warrior.feel(:left).empty?
							puts "walk_2"
							warrior.walk!(:left)
						elsif warrior.feel(:right).empty?
							puts "walk_3"
							warrior.walk!(:right)
						elsif warrior.feel(:forward).empty?
							puts "walk_4"
							warrior.walk!(:forward)
						elsif warrior.feel(:left).enemy?
							warrior.bind!(:left)
						elsif warrior.feel(:right).enemy?
							warrior.bind!(:right)
						elsif warrior.feel(:forward).enemy?
							wLook = warrior.look
							if wLook[0].enemy? && wLook[1].enemy?
								warrior.detonate!
							elsif wLook[0].captive? && wLook[1].enemy?
								warrior.detonate!
							else
								puts "attack 3"
								warrior.attack!(:forward)
							end
						else
							return false
						end
					elsif warrior.feel(:left).wall? && warrior.feel(:right).wall? && simple_tick?(warrior)
						wLook = warrior.look
						puts wLook
						#先走一步
						if  wLook.size > 1 && wLook[0].empty? && wLook[1].enemy?
					   		warrior.walk!(warrior.direction_of(wLook[0]))
							return true
						elsif wLook.size > 1 && wLook[0].to_s != "Captive" && wLook[1].to_s != "Captive"
							if wLook[0].enemy? 
								warrior.bind!(warrior.direction_of(wLook[0]))
							elsif warrior.health < 5
								puts "detonate after rest"
								warrior.rest!
							else
								puts "detonate 2"
								warrior.detonate!
							end
							return true
						end
						return false
					else
						wListen = warrior.listen
						enemyCount = 0
						ticking = false
						captive = false
						wListen.each{|item|
							if item.enemy?
								enemyCount = enemyCount + 1
							elsif item.ticking?
								ticking = true
							elsif item.captive?
								captive = true
							end
						}
						if enemyCount > 2 && ticking
							warrior.detonate!
						elsif  warrior.feel(dir).empty? && !warrior.feel(dir).stairs?
							#先救人
							puts "walk_5"
							warrior.walk!(dir)
						elsif warrior.feel(:right).empty? && !warrior.feel(:right).stairs?
							warrior.walk!(right)
						elsif warrior.feel.empty? && !warrior.feel.stairs?
							puts "walk_6"
							warrior.walk!
						elsif warrior.feel(:left).empty? && !warrior.feel(:left).stairs?
							warrior.walk!(:left)
						else
							return false
						end
					end
				return true
				end
			}
		end
		return false
	end
	
 end
